package com.liuning.async.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@RestController
public class MailSendTest {

    @Autowired
    private JavaMailSenderImpl javaMailSender;


    /**
     * 发送一个简单的邮件
     * @return
     */
    @RequestMapping("/send")
    public String sentMail() {

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //主题
        mailMessage.setSubject("邮件发送测试服务");
        //发送的内容
        mailMessage.setText("用于测试邮件发送服务是否可以正常运行，最终的结果是怎么样的");
        //发送给谁
        // mailMessage.setTo("liuninglut@163.com");
        mailMessage.setTo("liu.ning@newland.com.cn");
        //哪个账号发送
        mailMessage.setFrom("353823205@qq.com");
        javaMailSender.send(mailMessage);

        return "简单邮件发送成功";
    }

    /**
     * 发送一个复杂的邮件
     * @return
     */
    @RequestMapping("/sendMime")
    public String sentMimeMail() throws MessagingException {

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //进行组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject("复杂邮件发送测试服务");
        helper.setText("<p style='color:red'>这是一个复杂邮件的测试服务，用于测试是否可以成功的发送一个复杂的邮件消息</p>",true);
        //添加附件
        helper.addAttachment("1.jpg",new File("C:\\Users\\LN\\Pictures\\Saved Pictures\\doctor01.jpg"));
        //发送给谁
        helper.setTo("liuninglut@163.com");
        //哪个账号发送
        helper.setFrom("353823205@qq.com");
        javaMailSender.send(mimeMessage);

        return "复杂邮件发送成功";
    }
}
